package vu.alfonsodiecko.stundenplanfhjena.gui;

import org.holoeverywhere.ArrayAdapter;
import org.holoeverywhere.widget.AdapterView;
import org.holoeverywhere.widget.AdapterView.OnItemSelectedListener;
import org.holoeverywhere.widget.Button;
import org.holoeverywhere.widget.Spinner;

import vu.alfonsodiecko.stundenplanfhjena.Project;
import vu.alfonsodiecko.stundenplanfhjena.R;
import vu.alfonsodiecko.stundenplanfhjena.SQLiteHelper;
import vu.alfonsodiecko.stundenplanfhjena.SharedPreferencesHelper;
import vu.alfonsodiecko.stundenplanfhjena.datentyp.studiengang.Studiengaenge;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;

public class FragmentStudentLoader extends SherlockFragment implements OnItemSelectedListener {
	private Spinner spinnerStudies;
	private Spinner spinnerSemester;
	private Spinner spinnerGruppe;
	private int selection1;
	private int selection2;
	private int selection3;
	private Context con;
	private SQLiteHelper db;
	private Studiengaenge stduiengaenge;
	private View v;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		con = activity.getApplicationContext();
		db = new SQLiteHelper(con);
		stduiengaenge = db.getStudienGaenge();
		db.close();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		if (stduiengaenge.getStudiengaengeListeText().size() == 0) {
			v = inflater.inflate(R.layout.error, container, false);
		} else {
			v = inflater.inflate(R.layout.studenten, container, false);
			Button button = (Button) v.findViewById(R.id.buttonSave);
			button.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					saveSettings(v);
				}
			});
			spinnerStudies = (Spinner) v.findViewById(R.id.spinnerStudies);
			ArrayAdapter<String> adapterStudies = new ArrayAdapter<String>(v.getContext(), R.layout.spinner_view,
					stduiengaenge.getStudiengaengeListeText());
			spinnerStudies.setAdapter(adapterStudies);

			spinnerStudies.setOnItemSelectedListener(this);

			spinnerSemester = (Spinner) v.findViewById(R.id.spinnerSemester);
			spinnerSemester.setOnItemSelectedListener(this);

			spinnerGruppe = (Spinner) v.findViewById(R.id.spinnerGruppe);
			spinnerGruppe.setOnItemSelectedListener(this);
			SharedPreferencesHelper spt = new SharedPreferencesHelper(con);
			String val1 = spt.getSharedPreferences(Project.Strings.StudSet);
			if (!val1.equals("")) {
				spinnerStudies.setSelection(getSelectionValue(spinnerStudies, val1));

			}
		}

		return v;
	}

	private int getSelectionValue(Spinner spinner, String diff) {
		int position = 0;
		String[] values = getValuesBySpinner(spinner.getAdapter());
		if (values != null) {
			for (String str : values) {
				if (str.equals(diff)) {
					return position;
				}
				position++;
			}
		}
		return 0;
	}

	private String[] getValuesBySpinner(SpinnerAdapter adapter) {
		if (adapter != null) {
			int size = adapter.getCount();
			String[] values = new String[size];
			int i = 0;
			while (i < size) {
				values[i] = (String) adapter.getItem(i);
				i++;
			}
			return values;
		}
		return null;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		switch (arg0.getId()) {
		case R.id.spinnerStudies:
			selection1 = arg2;
			ArrayAdapter<String> adapterSemester = new ArrayAdapter<String>(con, R.layout.spinner_view, stduiengaenge.get(arg2)
					.getSemesterListeText());
			spinnerSemester.setAdapter(adapterSemester);

			ArrayAdapter<String> adapterGruppen = new ArrayAdapter<String>(con, R.layout.spinner_view, stduiengaenge.get(arg2).get(0)
					.getGruppenListeText());
			spinnerGruppe.setAdapter(adapterGruppen);

			break;
		case R.id.spinnerSemester:

			selection2 = arg2;
			ArrayAdapter<String> adapterGruppen2 = new ArrayAdapter<String>(con, R.layout.spinner_view, stduiengaenge.get(selection1).get(arg2)
					.getGruppenListeText());
			spinnerGruppe.setAdapter(adapterGruppen2);

			break;
		case R.id.spinnerGruppe:
			selection3 = arg2;

			break;
		}
	}

	public void saveSettings(View view) {

		SharedPreferencesHelper spt = new SharedPreferencesHelper(con);
		String dbID = String.valueOf(stduiengaenge.get(selection1).get(selection2).get(selection3).getStudienID());
		spt.setSharedPreferences(Project.Strings.dbId, dbID);
		spt.setSharedPreferences(Project.Strings.dbChange, "true");
		spt.setSharedPreferences(Project.Strings.dbIDMode, "student");
		spt.setSharedPreferences(Project.Strings.StudSet, stduiengaenge.get(selection1).getStudiengangName());
		spt.setSharedPreferences(Project.Strings.SemesterSet, String.valueOf(stduiengaenge.get(selection1).get(selection2).getSemester()));
		spt.setSharedPreferences(Project.Strings.GruppeSet, String.valueOf(stduiengaenge.get(selection1).get(selection2).get(selection3).getGruppe()));
		Toast.makeText(
				con,
				"Settings Saved:\nStudiengang: " + stduiengaenge.get(selection1).getStudiengangNameShort() + "\nSemester: "
						+ stduiengaenge.get(selection1).get(selection2).getSemester() + "\nGruppe: "
						+ stduiengaenge.get(selection1).get(selection2).get(selection3).getGruppe(), Toast.LENGTH_LONG).show();
		spt = new SharedPreferencesHelper(con);
		spt.setSharedPreferences(Project.Strings.install, "done");
		this.startActivity(new Intent(con, TerminPlaner.class));
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}
}